/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.dto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceEnumField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletDataSource;

/**
 *
 */
public class ApplicationTreeDto implements DataTransferObject {
    public static final String DATA_SOURCE_ID = "applicationTreeDS";

    public enum Type {
        APPLICATION,
        OPTION,
        VERSION,
        INSTANCE,
        USER
    }

    public String key;
    public String id;
    public Type type;
    public String parentKey;
    public String name;
    public boolean canEdit;

    public ApplicationTreeDto() {
    }

    public static SmartPortletDataSource getDataSource(SmartPortlet portlet) {
        return getDataSource(portlet, DATA_SOURCE_ID);
    }

    public static SmartPortletDataSource getDataSource(SmartPortlet portlet, String id) {
        id = portlet.getDataSourceId(id);
        SmartPortletDataSource ds = (SmartPortletDataSource)DataSource.get(id);
        if (ds != null) {
            return ds;
        }
        ds = new SmartPortletDataSource(portlet);
        ds.setID(id);

        DataSourceField field;

        field = new DataSourceTextField("key", "Key");
        field.setPrimaryKey(true);
        ds.addField(field);

        field = new DataSourceTextField("id", "Id");
        ds.addField(field);

        field = new DataSourceEnumField("type", "Type");
        ds.addField(field);

        field = new DataSourceTextField("parentKey", "Parent Key");
        field.setForeignKey(id + ".key");
        ds.addField(field);

        field = new DataSourceTextField("name", "Name");
        ds.addField(field);

        field = new DataSourceBooleanField("canEdit", "Can Edit");
        field.setHidden(true);
        ds.addField(field);
        return ds;
    }

    public void copyFromRecord(Record from) {
        key = from.getAttributeAsString("key");
        id = from.getAttributeAsString("id");
        type = Type.valueOf(from.getAttributeAsString("type"));
        parentKey = from.getAttributeAsString("parentKey");
        name = from.getAttributeAsString("name");
        canEdit = from.getAttributeAsBoolean("canEdit");
    }

    public void copyToRecord(Record to) {
        to.setAttribute("key", key);
        to.setAttribute("id", id);
        to.setAttribute("type", type);
        to.setAttribute("parentKey", parentKey);
        to.setAttribute("name", name);
        to.setAttribute("canEdit", canEdit);
    }
}
